package com.gt.quality.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gt.quality.base.pojo.BaseLimitRequest;
import com.gt.quality.base.pojo.LayerListViewObject;
import com.gt.quality.config.SpringBeanUtil;
import com.gt.quality.system.entity.SysDictType;
import com.gt.quality.system.entity.vo.MenuTreeResponse;
import com.gt.quality.system.entity.vo.MenuTreeVo;
import com.gt.quality.system.mapper.SysDictTypeMapper;
import com.gt.quality.system.service.SysDictTypeService;
import com.gt.quality.utils.CreateTreeUtils;
import jakarta.annotation.Resource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.support.TransactionTemplate;

import java.util.ArrayList;
import java.util.List;

/**
 * 字典类型表(SysDictType)表服务实现类
 *
 * @author makejava
 * @since 2026-12-26 11:20:04
 */
@Service("sysDictTypeService")
public class SysDictTypeServiceImpl extends ServiceImpl<SysDictTypeMapper, SysDictType> implements SysDictTypeService {

    @Resource
    private SysDictTypeMapper sysDictTypeMapper;


    /**
     * Description:  分页查询
     *
     * @param limitRequest 分页查询参数
     * @author: makeJava
     * @date: 2026-12-26 11:20:04
     */
    @Override
    public IPage<SysDictType> queryLimitPage(BaseLimitRequest<SysDictType> limitRequest) {
        SysDictType requestBody = limitRequest.getRequestBody();
        IPage<SysDictType> page = getByPageParam(limitRequest);
        JSONObject thisObj = JSONObject.from(requestBody);
        QueryWrapper<SysDictType> queryWrapper = queryWrapperBuilder(thisObj, limitRequest);
        return sysDictTypeMapper.selectPage(page, queryWrapper);
    }

    /**
     * Description:  批量新增优化版
     *
     * @param entityList entityList
     * @author: makeJava
     * @date: 2026-12-26 11:20:04
     * @return: Integer
     */
    @Override
    public Integer saveBatchByEasyBaseMapper(List<SysDictType> entityList) {

        return sysDictTypeMapper.insertBatchSomeColumn(entityList);
    }

    @Override
    public LayerListViewObject buildTree() {
        LayerListViewObject viewObject = new LayerListViewObject();
        List<SysDictType> list = this.list(new QueryWrapper<>());
        List<MenuTreeResponse> menuTreeVoList = new ArrayList<>(list.size());
        for (SysDictType item : list) {
            MenuTreeResponse menuTreeVo = CreateTreeUtils.buildMenuTreeResponse(item.getDictId(), item.getParentDictId(), item.getDictName(), "layui-icon-template", item.getDictType());
            menuTreeVoList.add(menuTreeVo);
        }
        List<MenuTreeResponse> allMenu = CreateTreeUtils.createTree(menuTreeVoList);
        // 默认首节点选中
        if (CollUtil.isNotEmpty(allMenu)) {
            allMenu.getFirst().setChecked(true);
        }
        viewObject.setNextLayer(allMenu);
        viewObject.setCurrentLayer(null);
        return viewObject;
    }


    /**
     * 往上传递 SqlSessionFactory
     *
     * @return SqlSessionFactory
     */
    @Override
    public SqlSessionFactory getSqlSessionFactory() {
        return super.getSqlSessionFactory();
    }

    /**
     * 往上传递 Mapper
     *
     * @return Mapper
     */
    @Override
    public Class<?> getThisMapperClass() {
        return SysDictTypeMapper.class;
    }

    /**
     * 往上传递 Transaction
     *
     * @return Transaction
     */
    @Override
    public TransactionTemplate getSpringTx() {
        return SpringBeanUtil.getBean(TransactionTemplate.class);
    }
}


